﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeapenDireact : MonoBehaviour 
{
	private Camera cam;
	//private Transform transform;
	// Use this for initialization
	void Start () 
	{
		cam = Camera.main;
	}
	
	// Update is called once per frame
	void Update () 
	{
		//获取鼠标位置
		Vector3 mouse = Input.mousePosition;
    	Vector3 mouseWorld = cam.ScreenToWorldPoint(new Vector3(mouse.x, mouse.y,
    		cam.nearClipPlane));
    	mouseWorld.z = 0;

    	//获取武器指向的向量
    	Vector3 head = transform.position;
    	Vector3 end = GameObject.Find("Weapen").transform.position;
    	Vector3 direaction = end - head;
    	Vector3 playerToMouse = mouseWorld-head;
    	float angle = Vector3.Angle(direaction, playerToMouse);
    	// Debug.Log("Angle:"+angle);
    	Vector3 cross = Vector3.Cross(direaction, playerToMouse);//计算向量叉乘，叉乘的z为正则顺时针
    	//Debug.Log("mouse.X"+temp.x+" mouse.Y"+temp.y+" mouse.Z"+temp.z);
    	if(cross.z > 0)
    		transform.Rotate(0,0,angle);
    	else
    		transform.Rotate(0,0,-angle);
    	// Debug.Log(angle);
    	// transform.rotation = Quaternion.LookRotation(forward, Vector3.up);//
	}
	
}
